<?php
/**
 * User: hjd-yao
 * Date: 2019/1/28
 * Time: 17:17
 */

namespace Shop\Http\Controllers\Wap;


use App\Helpers\SelfResponse;
use Shop\Http\Controllers\BaseController;
use App\Models\Cms\ArticleCatModel;
use App\Models\Cms\ArticleModel;
use App\Models\Cms\NoticeModel;

use Illuminate\Http\Request;

class ArticleController extends BaseController
{
    private static $_articleModel;
    private static $_noticeModel;
    private static $_articleCatModel;
    public function __construct()
    {
        self::$_articleModel = ArticleModel::init();
        self::$_noticeModel = NoticeModel::init();
        self::$_articleCatModel = ArticleCatModel::init();
    }

    /**
     * @title 文章/公告列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {

        $type= $request->input('type',2);

        switch ($type){
            case 1:
                $cmsModel = self::$_articleModel->with('articleCat')->where('is_open',1);
                break;
            case 2:
                $cmsModel = self::$_noticeModel->where('is_open',1);
                break;
            default:
                $cmsModel = self::$_noticeModel->where('is_open',1);
                break;
        }

        $pageNum = $request->input('page_num',1);
        $list = $cmsModel->forPage($pageNum,10)->get();

        return $this->wapRender('article.index',[
            'page_title'=>config('shop.cms')[$type],
            'list'=>$list,
            'type'=>$type,
        ]);

    }

    /**
     * @title 文章/公告详情
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function articleDetail(Request $request){

        $type= $request->input('type',2);
        $id = $request->input('id');
        switch ($type){
            case 1:
                $cmsModel = self::$_articleModel->with('articleCat')->where('is_open',1);
                break;
            case 2:
                $cmsModel = self::$_noticeModel->where('is_open',1);
                break;
            default:
                $cmsModel = self::$_noticeModel->where('is_open',1);
                break;
        }
        $detail_info  = $cmsModel->find($id);


    //    $detail_info['content'] =preg_replace("/(\s|\&nbsp\;|　|\xc2\xa0)/", " ", strip_tags($detail_info['content']));

        return $this->wapRender('article.detail',[
            'page_title'=>'详情',
            'detail_info'=>$detail_info,
            'type'=>$type,
        ]);
    }


    public function articleAll(Request $request){
        $catModel = self::$_articleCatModel->where('is_nav',1)->where('parent_id',0);
        $list = $catModel->get();
        return $this->wapRender('article.cat_all',[
            'page_title'=>'信息资讯',
            'list'=>$list,
        ]);
    }

    public function articleAllInfo(Request $request,$cat_id){

        $cat_title =  self::$_articleCatModel->where('id',$cat_id)->value('title');

        $cmsModel = self::$_articleModel->with('articleCat')->where('is_open',1)->where('cat_id',$cat_id);
        $pageNum = $request->input('page_num',1);
        $list = $cmsModel->forPage($pageNum,10)->get();

        return $this->wapRender('article.cat_all_info',[
            'page_title'=>$cat_title,
            'list'=>$list,
            'type'=>1,
        ]);
    }


}